﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.IO
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class CommandSet
        Implements IDisposable
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider)
            Me.serviceProvider = serviceProvider
            Me.menuCommandService = DirectCast(Me.serviceProvider.GetService(GetType(IMenuCommandService)), IMenuCommandService)
            If (Me.menuCommandService Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(IMenuCommandService).FullName }))
            End If
            Me.workflowView = [TryCast](serviceProvider.GetService(GetType(WorkflowView)),WorkflowView)
            If (Me.workflowView Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(WorkflowView).FullName }))
            End If
            Me.selectionService = DirectCast(Me.serviceProvider.GetService(GetType(ISelectionService)), ISelectionService)
            If (Me.selectionService Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ISelectionService).FullName }))
            End If
            Me.commandSet = New List([Of] CommandSetItem)
            Me.commandSet.AddRange(New CommandSetItem() { New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnMenuSaveWorkflowAsImage), WorkflowMenuCommands.SaveAsImage), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnMenuCopyToClipboard), WorkflowMenuCommands.CopyToClipboard), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusPrint), New EventHandler(AddressOf Me.OnMenuPrint), WorkflowMenuCommands.Print), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusPageSetup), New EventHandler(AddressOf Me.OnMenuPageSetup), WorkflowMenuCommands.PageSetup), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusDelete), New EventHandler(AddressOf Me.OnMenuDelete), StandardCommands.Delete), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusCopy), New EventHandler(AddressOf Me.OnMenuCopy), StandardCommands.Copy), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusCut), New EventHandler(AddressOf Me.OnMenuCut), StandardCommands.Cut), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusPaste), New EventHandler(AddressOf Me.OnMenuPaste), StandardCommands.Paste, True), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnMenuSelectAll), StandardCommands.SelectAll), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnMenuDesignerProperties), WorkflowMenuCommands.DesignerProperties), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnViewCode), New CommandID(StandardCommands.Cut.Guid, &H14D)), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyCancel), MenuCommands.KeyCancel), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyCancel), MenuCommands.KeyReverseCancel), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyMove), MenuCommands.KeyMoveUp), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyMove), MenuCommands.KeyMoveDown), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyMove), MenuCommands.KeyMoveLeft), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyMove), MenuCommands.KeyMoveRight), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyMove), MenuCommands.KeySelectNext), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyMove), MenuCommands.KeySelectPrevious), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusExpandCollapse), New EventHandler(AddressOf Me.OnExpandCollapse), WorkflowMenuCommands.Expand), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusExpandCollapse), New EventHandler(AddressOf Me.OnExpandCollapse), WorkflowMenuCommands.Collapse), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusEnable), New EventHandler(AddressOf Me.OnEnable), WorkflowMenuCommands.Disable, True), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusEnable), New EventHandler(AddressOf Me.OnEnable), WorkflowMenuCommands.Enable, True), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnCreateTheme), WorkflowMenuCommands.CreateTheme), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnChangeTheme), WorkflowMenuCommands.ChangeTheme), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAnySelection), New EventHandler(AddressOf Me.OnKeyDefault), MenuCommands.KeyDefaultAction), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyPageDnUp), WorkflowMenuCommands.PageUp), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusAlways), New EventHandler(AddressOf Me.OnKeyPageDnUp), WorkflowMenuCommands.PageDown) })
            Me.zoomCommands = New CommandSetItem() { New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.Zoom400Mode, DR.GetString("Zoom400Mode", New Object(0  - 1) {})), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.Zoom300Mode, DR.GetString("Zoom300Mode", New Object(0  - 1) {})), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.Zoom200Mode, DR.GetString("Zoom200Mode", New Object(0  - 1) {})), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.Zoom150Mode, DR.GetString("Zoom150Mode", New Object(0  - 1) {})), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.Zoom100Mode, DR.GetString("Zoom100Mode", New Object(0  - 1) {})), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.Zoom75Mode, DR.GetString("Zoom75Mode", New Object(0  - 1) {})), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.Zoom50Mode, DR.GetString("Zoom50Mode", New Object(0  - 1) {})), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusZoom), New EventHandler(AddressOf Me.OnZoom), WorkflowMenuCommands.ShowAll, DR.GetString("ZoomShowAll", New Object(0  - 1) {})) }
            Me.commandSet.AddRange(Me.zoomCommands)
            Me.layoutCommands = New CommandSetItem() { New CommandSetItem(New EventHandler(AddressOf Me.OnStatusLayout), New EventHandler(AddressOf Me.OnPageLayout), WorkflowMenuCommands.DefaultPage), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusLayout), New EventHandler(AddressOf Me.OnPageLayout), WorkflowMenuCommands.PrintPreviewPage), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusLayout), New EventHandler(AddressOf Me.OnPageLayout), WorkflowMenuCommands.PrintPreview) }
            Me.commandSet.AddRange(Me.layoutCommands)
            Me.navigationToolCommands = New CommandSetItem() { New CommandSetItem(New EventHandler(AddressOf Me.OnStatusMessageFilter), New EventHandler(AddressOf Me.OnMessageFilterChanged), CommandSet.NavigationToolCommandIds(0)), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusMessageFilter), New EventHandler(AddressOf Me.OnMessageFilterChanged), CommandSet.NavigationToolCommandIds(1)), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusMessageFilter), New EventHandler(AddressOf Me.OnMessageFilterChanged), CommandSet.NavigationToolCommandIds(2)), New CommandSetItem(New EventHandler(AddressOf Me.OnStatusMessageFilter), New EventHandler(AddressOf Me.OnMessageFilterChanged), CommandSet.NavigationToolCommandIds(3)) }
            Me.commandSet.AddRange(Me.navigationToolCommands)
            Dim i As Integer
            For i = 0 To Me.commandSet.Count - 1
                If (Me.menuCommandService.FindCommand(Me.commandSet.Item(i).CommandID) Is Nothing) Then
                    Me.menuCommandService.AddCommand(Me.commandSet.Item(i))
                End If
            Next i
            Dim service As IComponentChangeService = [TryCast](Me.serviceProvider.GetService(GetType(IComponentChangeService)),IComponentChangeService)
            If (Not service Is Nothing) Then
                AddHandler service.ComponentChanged, New ComponentChangedEventHandler(AddressOf Me.OnComponentChanged)
            End If
            Dim service2 As IDictionaryService = [TryCast](Me.serviceProvider.GetService(GetType(IDictionaryService)),IDictionaryService)
            If (Not service2 Is Nothing) Then
                service2.SetValue(GetType(CommandID), New CommandID(New Guid("5f1c3c8d-60f1-4b98-b85b-8679f97e8eac"), 0))
            End If
        End Sub

        Private Function ConvertMessageFilterToCommandID() As CommandID
            If TypeOf Me.activeFilter Is PanningMessageFilter Then
                Return WorkflowMenuCommands.Pan
            End If
            If Not TypeOf Me.activeFilter Is ZoomingMessageFilter Then
                Return WorkflowMenuCommands.DefaultFilter
            End If
            If DirectCast(Me.activeFilter, ZoomingMessageFilter).ZoomingIn Then
                Return WorkflowMenuCommands.ZoomIn
            End If
            Return WorkflowMenuCommands.ZoomOut
        End Function

        Private Function ConvertToZoomCommand(ByVal zoomLevel As Integer) As Integer
            Dim iD As Integer = 0
            If (zoomLevel = 400) Then
                Return WorkflowMenuCommands.Zoom400Mode.ID
            End If
            If (zoomLevel = 300) Then
                Return WorkflowMenuCommands.Zoom300Mode.ID
            End If
            If (zoomLevel = 200) Then
                Return WorkflowMenuCommands.Zoom200Mode.ID
            End If
            If (zoomLevel = 150) Then
                Return WorkflowMenuCommands.Zoom150Mode.ID
            End If
            If (zoomLevel = 100) Then
                Return WorkflowMenuCommands.Zoom100Mode.ID
            End If
            If (zoomLevel = &H4B) Then
                Return WorkflowMenuCommands.Zoom75Mode.ID
            End If
            If (zoomLevel = 50) Then
                iD = WorkflowMenuCommands.Zoom50Mode.ID
            End If
            Return iD
        End Function

        Private Function ConvertToZoomLevel(ByVal commandId As Integer) As Integer
            Dim num As Integer = 100
            If (commandId = WorkflowMenuCommands.Zoom400Mode.ID) Then
                Return 400
            End If
            If (commandId = WorkflowMenuCommands.Zoom300Mode.ID) Then
                Return 300
            End If
            If (commandId = WorkflowMenuCommands.Zoom200Mode.ID) Then
                Return 200
            End If
            If (commandId = WorkflowMenuCommands.Zoom150Mode.ID) Then
                Return 150
            End If
            If (commandId = WorkflowMenuCommands.Zoom100Mode.ID) Then
                Return 100
            End If
            If (commandId = WorkflowMenuCommands.Zoom75Mode.ID) Then
                Return &H4B
            End If
            If (commandId = WorkflowMenuCommands.Zoom50Mode.ID) Then
                num = 50
            End If
            Return num
        End Function

        Public Sub Dispose()
            Dim service As IComponentChangeService = [TryCast](Me.serviceProvider.GetService(GetType(IComponentChangeService)),IComponentChangeService)
            If (Not service Is Nothing) Then
                RemoveHandler service.ComponentChanged, New ComponentChangedEventHandler(AddressOf Me.OnComponentChanged)
            End If
            If (Not Me.activeFilter Is Nothing) Then
                Me.workflowView.RemoveDesignerMessageFilter(Me.activeFilter)
                Me.activeFilter.Dispose
                Me.activeFilter = Nothing
            End If
            Me.selectionService = Nothing
            Dim i As Integer
            For i = 0 To Me.commandSet.Count - 1
                Me.menuCommandService.RemoveCommand(Me.commandSet.Item(i))
            Next i
            Me.menuCommandService = Nothing
        End Sub

        Private Sub OnChangeTheme(ByVal sender As Object, ByVal e As EventArgs)
            Dim service As IExtendedUIService = [TryCast](Me.serviceProvider.GetService(GetType(IExtendedUIService)),IExtendedUIService)
            If (Not service Is Nothing) Then
                service.ShowToolsOptions
            End If
        End Sub

        Private Sub OnComponentChanged(ByVal sender As Object, ByVal e As ComponentChangedEventArgs)
            If (Not Me.activeFilter Is Nothing) Then
                Me.workflowView.RemoveDesignerMessageFilter(Me.activeFilter)
                Me.activeFilter = Nothing
                Me.UpdatePanCommands(True)
            End If
        End Sub

        Private Sub OnCreateTheme(ByVal sender As Object, ByVal e As EventArgs)
            Dim dialog As New ThemeConfigurationDialog(Me.serviceProvider)
            If (dialog.ShowDialog = DialogResult.OK) Then
                Dim theme As WorkflowTheme = dialog.ComposedTheme.Clone
                If (Not theme Is Nothing) Then
                    WorkflowTheme.CurrentTheme = theme
                    WorkflowTheme.SaveThemeSettingToRegistry
                End If
            End If
        End Sub

        Private Sub OnEnable(ByVal sender As Object, ByVal e As EventArgs)
            Dim command1 As MenuCommand = DirectCast(sender, MenuCommand)
            Dim transaction As DesignerTransaction = Nothing
            Dim primarySelection As IComponent = [TryCast](Me.selectionService.PrimarySelection,IComponent)
            If ((Not primarySelection Is Nothing) AndAlso (Not primarySelection.Site Is Nothing)) Then
                Dim service As IDesignerHost = [TryCast](primarySelection.Site.GetService(GetType(IDesignerHost)),IDesignerHost)
                If (Not service Is Nothing) Then
                    transaction = service.CreateTransaction(SR.GetString("ChangingEnabled"))
                End If
            End If
            Try 
                Dim obj2 As Object
                For Each obj2 In Me.selectionService.GetSelectedComponents
                    Dim activity As Activity = [TryCast](obj2,Activity)
                    If (Not activity Is Nothing) Then
                        Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(activity)
                        If ((Not designer Is Nothing) AndAlso Not designer.IsLocked) Then
                            Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties(activity).Item("Enabled")
                            If (Not descriptor Is Nothing) Then
                                descriptor.SetValue(activity, Not activity.Enabled)
                            End If
                        End If
                    End If
                Next
                If (Not transaction Is Nothing) Then
                    transaction.Commit
                End If
            Finally
                If (Not transaction Is Nothing) Then
                    DirectCast(transaction, IDisposable).Dispose
                End If
            End Try
            Dim command As MenuCommand = Me.menuCommandService.FindCommand(WorkflowMenuCommands.Disable)
            If (Not command Is Nothing) Then
                Me.OnStatusEnable(command, EventArgs.Empty)
            End If
            Dim command2 As MenuCommand = Me.menuCommandService.FindCommand(WorkflowMenuCommands.Enable)
            If (Not command2 Is Nothing) Then
                Me.OnStatusEnable(command2, EventArgs.Empty)
            End If
        End Sub

        Private Sub OnExpandCollapse(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Dim obj2 As Object
            For Each obj2 In Me.selectionService.GetSelectedComponents
                Dim activity As Activity = [TryCast](obj2,Activity)
                If (Not activity Is Nothing) Then
                    Dim designer As CompositeActivityDesigner = [TryCast](ActivityDesigner.GetDesigner(activity),CompositeActivityDesigner)
                    If (Not designer Is Nothing) Then
                        designer.Expanded = (command.CommandID.ID = WorkflowMenuCommands.Expand.ID)
                    End If
                End If
            Next
            Dim command2 As MenuCommand = Me.menuCommandService.FindCommand(WorkflowMenuCommands.Expand)
            If (Not command2 Is Nothing) Then
                Me.OnStatusExpandCollapse(command2, EventArgs.Empty)
            End If
            Dim command3 As MenuCommand = Me.menuCommandService.FindCommand(WorkflowMenuCommands.Collapse)
            If (Not command3 Is Nothing) Then
                Me.OnStatusExpandCollapse(command3, EventArgs.Empty)
            End If
        End Sub

        Private Sub OnKeyCancel(ByVal sender As Object, ByVal e As EventArgs)
            Me.SendKeyDownCommand(Keys.Escape)
        End Sub

        Private Sub OnKeyDefault(ByVal sender As Object, ByVal e As EventArgs)
            Me.SendKeyDownCommand(Keys.Return)
        End Sub

        Private Sub OnKeyMove(ByVal sender As Object, ByVal e As EventArgs)
            If (Not Me.selectionService.PrimarySelection Is Nothing) Then
                Dim command As MenuCommand = DirectCast(sender, MenuCommand)
                Dim left As Keys = Keys.Left
                If (command.CommandID.ID = MenuCommands.KeyMoveDown.ID) Then
                    left = Keys.Down
                ElseIf (command.CommandID.ID = MenuCommands.KeyMoveUp.ID) Then
                    left = Keys.Up
                ElseIf (command.CommandID.ID = MenuCommands.KeyMoveLeft.ID) Then
                    left = Keys.Left
                ElseIf (command.CommandID.ID = MenuCommands.KeyMoveRight.ID) Then
                    left = Keys.Right
                ElseIf (command.CommandID.ID = MenuCommands.KeySelectNext.ID) Then
                    left = Keys.Tab
                ElseIf (command.CommandID.ID = MenuCommands.KeySelectPrevious.ID) Then
                    left = (Keys.Shift Or Keys.Tab)
                End If
                Me.SendKeyDownCommand(left)
            End If
        End Sub

        Private Sub OnKeyPageDnUp(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Me.SendKeyDownCommand(If((command.CommandID Is WorkflowMenuCommands.PageUp), Keys.Prior, Keys.Next))
        End Sub

        Private Sub OnMenuCopy(ByVal sender As Object, ByVal e As EventArgs)
            If Helpers.AreAllActivities(Me.selectionService.GetSelectedComponents) Then
                Dim topLevelActivities As Activity() = Helpers.GetTopLevelActivities(Me.selectionService.GetSelectedComponents)
                Clipboard.SetDataObject(CompositeActivityDesigner.SerializeActivitiesToDataObject(Me.serviceProvider, topLevelActivities))
            End If
        End Sub

        Private Sub OnMenuCopyToClipboard(ByVal sender As Object, ByVal e As EventArgs)
            Me.workflowView.SaveWorkflowImageToClipboard
        End Sub

        Private Sub OnMenuCut(ByVal sender As Object, ByVal e As EventArgs)
            Dim service As IDesignerHost = [TryCast](Me.serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
            If ((service Is Nothing) OrElse Not Me.selectionService.GetComponentSelected(service.RootComponent)) Then
                Dim selectedComponents As ICollection = Me.selectionService.GetSelectedComponents
                If (Helpers.AreAllActivities(selectedComponents) AndAlso DesignerHelpers.AreAssociatedDesignersMovable(selectedComponents)) Then
                    Me.OnMenuCopy(sender, e)
                    Dim description As String = String.Empty
                    If (selectedComponents.Count > 1) Then
                        description = SR.GetString("CutMultipleActivities", New Object() { selectedComponents.Count })
                    Else
                        Dim list As New ArrayList(selectedComponents)
                        If (list.Count > 0) Then
                            description = SR.GetString("CutSingleActivity", New Object() { [TryCast](list.Item(0),Activity).Name })
                        Else
                            description = SR.GetString("CutActivity")
                        End If
                    End If
                    Dim transaction As DesignerTransaction = service.CreateTransaction(description)
                    Try 
                        Me.OnMenuDelete(sender, e)
                        transaction.Commit
                    Catch obj1 As Object
                        transaction.Cancel
                    End Try
                End If
            End If
        End Sub

        Private Sub OnMenuDelete(ByVal sender As Object, ByVal e As EventArgs)
            Me.SendKeyDownCommand(Keys.Delete)
        End Sub

        Private Sub OnMenuDesignerProperties(ByVal sender As Object, ByVal e As EventArgs)
            If (Not Me.menuCommandService Is Nothing) Then
                Me.menuCommandService.GlobalInvoke(StandardCommands.PropertiesWindow)
            End If
        End Sub

        Private Sub OnMenuPageSetup(ByVal sender As Object, ByVal e As EventArgs)
            If (PrinterSettings.InstalledPrinters.Count < 1) Then
                DesignerHelpers.ShowError(Me.serviceProvider, DR.GetString("ThereIsNoPrinterInstalledErrorMessage", New Object(0  - 1) {}))
            Else
                Dim dialog As New WorkflowPageSetupDialog(Me.serviceProvider)
                If (DialogResult.OK = dialog.ShowDialog) Then
                    Me.workflowView.PerformLayout(False)
                End If
            End If
        End Sub

        Private Sub OnMenuPaste(ByVal sender As Object, ByVal e As EventArgs)
            Dim primarySelection As Object = Me.selectionService.PrimarySelection
            Dim parentDesigner As CompositeActivityDesigner = [TryCast](ActivityDesigner.GetDesigner([TryCast](primarySelection,Activity)),CompositeActivityDesigner)
            If (parentDesigner Is Nothing) Then
                parentDesigner = ActivityDesigner.GetParentDesigner(primarySelection)
            End If
            If ((Not parentDesigner Is Nothing) AndAlso parentDesigner.IsEditable) Then
                Dim dataObject As IDataObject = Clipboard.GetDataObject
                Dim activities As ICollection = Nothing
                Try 
                    activities = CompositeActivityDesigner.DeserializeActivitiesFromDataObject(Me.serviceProvider, dataObject, True)
                Catch exception As Exception
                    If (Not exception Is CheckoutException.Canceled) Then
                        Throw New Exception((DR.GetString("ActivityInsertError", New Object(0  - 1) {}) & ChrW(10) & exception.Message), exception)
                    End If
                End Try
                If (activities Is Nothing) Then
                    Throw New InvalidOperationException(DR.GetString("InvalidOperationBadClipboardFormat", New Object(0  - 1) {}))
                End If
                Dim insertLocation As HitTestInfo = Nothing
                If TypeOf primarySelection Is HitTestInfo Then
                    insertLocation = DirectCast(primarySelection, HitTestInfo)
                ElseIf TypeOf primarySelection Is CompositeActivity Then
                    insertLocation = New HitTestInfo(parentDesigner, HitTestLocations.Designer)
                ElseIf TypeOf primarySelection Is Activity Then
                    Dim item As Activity = [TryCast](primarySelection,Activity)
                    Dim parent As CompositeActivity = item.Parent
                    Dim designer As CompositeActivityDesigner = [TryCast](ActivityDesigner.GetDesigner(parent),CompositeActivityDesigner)
                    If (Not designer Is Nothing) Then
                        insertLocation = New ConnectorHitTestInfo(designer, HitTestLocations.Designer, (parent.Activities.IndexOf(item) + 1))
                    End If
                End If
                Dim list As New List([Of] Activity)(Helpers.GetTopLevelActivities(activities))
                If ((insertLocation Is Nothing) OrElse Not parentDesigner.CanInsertActivities(insertLocation, list.AsReadOnly)) Then
                    Throw New Exception(SR.GetString("Error_NoPasteSupport"))
                End If
                Dim service As IExtendedUIService = [TryCast](Me.serviceProvider.GetService(GetType(IExtendedUIService)),IExtendedUIService)
                If (Not service Is Nothing) Then
                    Dim activity3 As Activity
                    For Each activity3 In activities
                        service.AddAssemblyReference(activity3.GetType.Assembly.GetName)
                    Next
                End If
                CompositeActivityDesigner.InsertActivities(parentDesigner, insertLocation, list.AsReadOnly, SR.GetString("PastingActivities"))
                Dim data As Stream = [TryCast](dataObject.GetData("CF_WINOEDESIGNERCOMPONENTSSTATE"),Stream)
                If (Not data Is Nothing) Then
                    Helpers.DeserializeDesignersFromStream(activities, data)
                End If
                Me.selectionService.SetSelectedComponents(list.ToArray, SelectionTypes.Replace)
                Me.workflowView.EnsureVisible(Me.selectionService.PrimarySelection)
            End If
        End Sub

        Private Sub OnMenuPrint(ByVal sender As Object, ByVal e As EventArgs)
            If (PrinterSettings.InstalledPrinters.Count < 1) Then
                DesignerHelpers.ShowError(Me.serviceProvider, DR.GetString("ThereIsNoPrinterInstalledErrorMessage", New Object(0  - 1) {}))
            Else
                Dim printDocument As PrintDocument = Me.workflowView.PrintDocument
                Dim dialog As New PrintDialog { _
                    .AllowPrintToFile = False, _
                    .Document = printDocument _
                }
                Try 
                    If (DialogResult.OK = dialog.ShowDialog) Then
                        Dim printerSettings As PrinterSettings = printDocument.PrinterSettings
                        Dim defaultPageSettings As PageSettings = printDocument.DefaultPageSettings
                        printDocument.PrinterSettings = dialog.PrinterSettings
                        printDocument.DefaultPageSettings = dialog.Document.DefaultPageSettings
                        printDocument.Print
                        printDocument.PrinterSettings = printerSettings
                        printDocument.DefaultPageSettings = defaultPageSettings
                    End If
                Catch exception As Exception
                    Dim message As String = (DR.GetString("SelectedPrinterIsInvalidErrorMessage", New Object(0  - 1) {}) & ChrW(10) & exception.Message)
                    DesignerHelpers.ShowError(Me.serviceProvider, message)
                End Try
            End If
        End Sub

        Private Sub OnMenuSaveWorkflowAsImage(ByVal sender As Object, ByVal e As EventArgs)
            Dim list As New List([Of] SupportedImageFormats) { New SupportedImageFormats(DR.GetString("BMPImageFormat", New Object(0  - 1) {}), ImageFormat.Bmp), New SupportedImageFormats(DR.GetString("JPEGImageFormat", New Object(0  - 1) {}), ImageFormat.Jpeg), New SupportedImageFormats(DR.GetString("PNGImageFormat", New Object(0  - 1) {}), ImageFormat.Png), New SupportedImageFormats(DR.GetString("TIFFImageFormat", New Object(0  - 1) {}), ImageFormat.Tiff), New SupportedImageFormats(DR.GetString("WMFImageFormat", New Object(0  - 1) {}), ImageFormat.Wmf), New SupportedImageFormats(DR.GetString("EXIFImageFormat", New Object(0  - 1) {}), ImageFormat.Exif), New SupportedImageFormats(DR.GetString("EMFImageFormat", New Object(0  - 1) {}), ImageFormat.Emf) }
            Dim dialog As New SaveFileDialog { _
                .Title = DR.GetString("SaveWorkflowImageDialogTitle", New Object(0  - 1) {}), _
                .DefaultExt = "bmp" _
            }
            Dim str As String = String.Empty
            Dim formats As SupportedImageFormats
            For Each formats In list
                str = (str & If((str.Length > 0), ("|" & formats.Description), formats.Description))
            Next
            dialog.Filter = str
            dialog.FilterIndex = 0
            If (((dialog.ShowDialog = DialogResult.OK) AndAlso (dialog.FilterIndex > 0)) AndAlso (dialog.FilterIndex <= list.Count)) Then
                Me.workflowView.SaveWorkflowImage(dialog.FileName, list.Item((dialog.FilterIndex - 1)).Format)
            End If
        End Sub

        Private Sub OnMenuSelectAll(ByVal sender As Object, ByVal e As EventArgs)
            Dim safeRootDesigner As ActivityDesigner = ActivityDesigner.GetSafeRootDesigner(Me.serviceProvider)
            If (Not safeRootDesigner Is Nothing) Then
                Dim list As New List([Of] Activity)
                If TypeOf safeRootDesigner.Activity Is CompositeActivity Then
                    list.AddRange(Helpers.GetNestedActivities([TryCast](safeRootDesigner.Activity,CompositeActivity)))
                End If
                Me.selectionService.SetSelectedComponents(list.ToArray, SelectionTypes.Replace)
            End If
        End Sub

        Private Sub OnMessageFilterChanged(ByVal sender As Object, ByVal e As EventArgs)
            If (Not Me.activeFilter Is Nothing) Then
                Me.workflowView.RemoveDesignerMessageFilter(Me.activeFilter)
                Me.activeFilter = Nothing
            End If
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Dim iD As Integer = command.CommandID.ID
            If (WorkflowMenuCommands.ZoomIn.ID = iD) Then
                Me.activeFilter = New ZoomingMessageFilter(True)
            ElseIf (WorkflowMenuCommands.ZoomOut.ID = iD) Then
                Me.activeFilter = New ZoomingMessageFilter(False)
            ElseIf (WorkflowMenuCommands.Pan.ID = iD) Then
                Me.activeFilter = New PanningMessageFilter
            End If
            If (Not Me.activeFilter Is Nothing) Then
                Me.workflowView.AddDesignerMessageFilter(Me.activeFilter)
            End If
            Me.workflowView.Focus
            Me.UpdatePanCommands(True)
        End Sub

        Private Sub OnPageLayout(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Me.workflowView.PrintPreviewMode = If((command.CommandID Is WorkflowMenuCommands.PrintPreview), Not Me.workflowView.PrintPreviewMode, (command.CommandID Is WorkflowMenuCommands.PrintPreviewPage))
            Me.UpdatePageLayoutCommands(True)
        End Sub

        Private Sub OnStatusAlways(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            command.Enabled = True
        End Sub

        Private Sub OnStatusAnySelection(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Dim service As IDesignerHost = [TryCast](Me.serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
            command.Enabled = (((Not service Is Nothing) AndAlso (Me.selectionService.GetSelectedComponents.Count > 0)) AndAlso Not Me.selectionService.GetComponentSelected(service.RootComponent))
        End Sub

        Private Sub OnStatusCopy(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Dim flag As Boolean = False
            Dim service As IDesignerHost = [TryCast](Me.serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
            If ((Not service Is Nothing) AndAlso Not service.Loading) Then
                Dim c As New ArrayList(Me.selectionService.GetSelectedComponents)
                flag = Helpers.AreAllActivities(c)
                If flag Then
                    Dim activity As Activity
                    For Each activity In c
                        If (Not activity.Site Is Nothing) Then
                            service = [TryCast](activity.Site.GetService(GetType(IDesignerHost)),IDesignerHost)
                            If ((Not service Is Nothing) AndAlso Me.selectionService.GetComponentSelected(service.RootComponent)) Then
                                flag = False
                                Exit For
                            End If
                        End If
                    Next
                End If
            End If
            command.Enabled = flag
        End Sub

        Private Sub OnStatusCut(ByVal sender As Object, ByVal e As EventArgs)
            Me.OnStatusDelete(sender, e)
        End Sub

        Private Sub OnStatusDelete(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            command.Enabled = False
            Dim service As IDesignerHost = [TryCast](Me.serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
            If (((service Is Nothing) OrElse (service.RootComponent Is Nothing)) OrElse Not Me.selectionService.GetComponentSelected(service.RootComponent)) Then
                Dim selectedComponents As ICollection = Me.selectionService.GetSelectedComponents
                If DesignerHelpers.AreComponentsRemovable(selectedComponents) Then
                    Dim entry As DictionaryEntry
                    For Each entry In Helpers.PairUpCommonParentActivities(Helpers.GetTopLevelActivities(selectedComponents))
                        Dim designer As CompositeActivityDesigner = [TryCast](ActivityDesigner.GetDesigner([TryCast](entry.Key,Activity)),CompositeActivityDesigner)
                        If ((Not designer Is Nothing) AndAlso Not designer.CanRemoveActivities(New List([Of] Activity)(DirectCast(DirectCast(entry.Value, ArrayList).ToArray(GetType(Activity)), Activity())).AsReadOnly)) Then
                            command.Enabled = False
                            Return
                        End If
                    Next
                    command.Enabled = True
                End If
            End If
        End Sub

        Private Sub OnStatusEnable(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Dim enabled As Boolean = True
            Dim flag2 As Boolean = True
            Dim list As New ArrayList(Me.selectionService.GetSelectedComponents)
            Dim i As Integer = 0
            Do While ((i < list.Count) AndAlso flag2)
                Dim activity As Activity = [TryCast](list.Item(i),Activity)
                If (Not activity Is Nothing) Then
                    Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(activity)
                    If ((((designer Is Nothing) OrElse designer.IsLocked) OrElse ((i > 0) AndAlso (enabled <> activity.Enabled))) OrElse ((Not Me.workflowView.RootDesigner Is Nothing) AndAlso (Me.workflowView.RootDesigner.Activity Is activity))) Then
                        flag2 = False
                    Else
                        enabled = activity.Enabled
                    End If
                Else
                    flag2 = False
                End If
                i += 1
            Loop
            command.Visible = command.Enabled = (flag2 AndAlso (((command.CommandID Is WorkflowMenuCommands.Enable) AndAlso Not enabled) OrElse ((command.CommandID Is WorkflowMenuCommands.Disable) AndAlso enabled)))
        End Sub

        Private Sub OnStatusExpandCollapse(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            Dim num As Integer = 0
            Dim obj2 As Object
            For Each obj2 In Me.selectionService.GetSelectedComponents
                Dim activity As Activity = [TryCast](obj2,Activity)
                If (Not activity Is Nothing) Then
                    Dim designer As CompositeActivityDesigner = [TryCast](ActivityDesigner.GetDesigner(activity),CompositeActivityDesigner)
                    If (((Not designer Is Nothing) AndAlso designer.CanExpandCollapse) AndAlso (((command.CommandID Is WorkflowMenuCommands.Expand) AndAlso Not designer.Expanded) OrElse ((command.CommandID Is WorkflowMenuCommands.Collapse) AndAlso designer.Expanded))) Then
                        num += 1
                    End If
                End If
            Next
            command.Visible = command.Enabled = (num = Me.selectionService.SelectionCount)
        End Sub

        Private Sub OnStatusLayout(ByVal sender As Object, ByVal e As EventArgs)
            Me.UpdatePageLayoutCommands(True)
        End Sub

        Private Sub OnStatusMessageFilter(ByVal sender As Object, ByVal e As EventArgs)
            Me.UpdatePanCommands(True)
        End Sub

        Private Sub OnStatusPageSetup(ByVal sender As Object, ByVal e As EventArgs)
            Me.OnStatusAlways(sender, e)
        End Sub

        Private Sub OnStatusPaste(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            command.Enabled = False
            Dim primarySelection As Object = Me.selectionService.PrimarySelection
            Dim parentDesigner As CompositeActivityDesigner = [TryCast](ActivityDesigner.GetDesigner([TryCast](primarySelection,Activity)),CompositeActivityDesigner)
            If (parentDesigner Is Nothing) Then
                parentDesigner = ActivityDesigner.GetParentDesigner(primarySelection)
            End If
            If ((Not parentDesigner Is Nothing) AndAlso parentDesigner.IsEditable) Then
                Dim host As IDesignerHost = [TryCast](Me.serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
                Dim service As IToolboxService = DirectCast(Me.serviceProvider.GetService(GetType(IToolboxService)), IToolboxService)
                Dim dataObject As IDataObject = Clipboard.GetDataObject
                If (((Not dataObject Is Nothing) AndAlso (Not host Is Nothing)) AndAlso ((dataObject.GetDataPresent("CF_WINOEDESIGNERCOMPONENTS") OrElse (service Is Nothing)) OrElse service.IsSupported(dataObject, host))) Then
                    Dim insertLocation As HitTestInfo = Nothing
                    If TypeOf primarySelection Is HitTestInfo Then
                        insertLocation = DirectCast(primarySelection, HitTestInfo)
                    ElseIf TypeOf primarySelection Is CompositeActivity Then
                        insertLocation = New HitTestInfo(parentDesigner, HitTestLocations.Designer)
                    ElseIf TypeOf primarySelection Is Activity Then
                        Dim item As Activity = [TryCast](primarySelection,Activity)
                        Dim parent As CompositeActivity = item.Parent
                        Dim designer As CompositeActivityDesigner = [TryCast](ActivityDesigner.GetDesigner(parent),CompositeActivityDesigner)
                        If (Not designer Is Nothing) Then
                            insertLocation = New ConnectorHitTestInfo(designer, HitTestLocations.Designer, (parent.Activities.IndexOf(item) + 1))
                        End If
                    End If
                    Dim activities As ICollection = Nothing
                    Try 
                        activities = CompositeActivityDesigner.DeserializeActivitiesFromDataObject(Me.serviceProvider, dataObject)
                    Catch exception As CheckoutException
                        If (Not exception Is CheckoutException.Canceled) Then
                            Throw exception
                        End If
                    End Try
                    command.Enabled = (((Not activities Is Nothing) AndAlso (Not insertLocation Is Nothing)) AndAlso parentDesigner.CanInsertActivities(insertLocation, New List([Of] Activity)(Helpers.GetTopLevelActivities(activities)).AsReadOnly))
                End If
            End If
        End Sub

        Private Sub OnStatusPrint(ByVal sender As Object, ByVal e As EventArgs)
            Me.OnStatusAlways(sender, e)
        End Sub

        Private Sub OnStatusZoom(ByVal sender As Object, ByVal e As EventArgs)
            Me.UpdateZoomCommands(True)
        End Sub

        Private Sub OnViewCode(ByVal sender As Object, ByVal e As EventArgs)
            Dim host As IDesignerHost = [TryCast](Me.serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
            Dim component As IComponent = If((Not host Is Nothing), host.RootComponent, Nothing)
            If (Not component Is Nothing) Then
                Dim service As IMemberCreationService = [TryCast](component.Site.GetService(GetType(IMemberCreationService)),IMemberCreationService)
                If (Not service Is Nothing) Then
                    service.ShowCode
                End If
            End If
        End Sub

        Private Sub OnZoom(ByVal sender As Object, ByVal e As EventArgs)
            Dim command As MenuCommand = DirectCast(sender, MenuCommand)
            If (command.CommandID.ID = WorkflowMenuCommands.ShowAll.ID) Then
                Dim num As Integer = CInt(((100! / Me.workflowView.ActiveLayout.Scaling) * Math.Min(CSng((CSng(Me.workflowView.ViewPortSize.Width) / CSng(Me.workflowView.ActiveLayout.Extent.Width))), CSng((CSng(Me.workflowView.ViewPortSize.Height) / CSng(Me.workflowView.ActiveLayout.Extent.Height))))))
                Me.workflowView.Zoom = Math.Min(Math.Max(num, 10), 400)
            Else
                Me.workflowView.Zoom = Me.ConvertToZoomLevel(command.CommandID.ID)
            End If
            Me.UpdateZoomCommands(True)
        End Sub

        Private Function SendKeyDownCommand(ByVal key As Keys) As Boolean
            Dim service As IDesignerHost = [TryCast](Me.serviceProvider.GetService(GetType(IDesignerHost)),IDesignerHost)
            If (Not service Is Nothing) Then
                Dim designer As IRootDesigner = ActivityDesigner.GetDesigner([TryCast](service.RootComponent,Activity))
                If (Not designer Is Nothing) Then
                    Dim view As WorkflowView = [TryCast](designer.GetView(ViewTechnology.Default),WorkflowView)
                    If (Not view Is Nothing) Then
                        Dim e As New KeyEventArgs(key)
                        view.OnCommandKey(e)
                        Return e.Handled
                    End If
                End If
            End If
            Return False
        End Function

        Friend Sub UpdateCommandSet()
            Dim i As Integer
            For i = 0 To Me.commandSet.Count - 1
                Me.commandSet.Item(i).UpdateStatus
            Next i
        End Sub

        Friend Sub UpdatePageLayoutCommands(ByVal enable As Boolean)
            Dim layoutCommands As CommandSetItem() = Me.layoutCommands
            Dim i As Integer
            For i = 0 To layoutCommands.Length - 1
                Dim command As MenuCommand = layoutCommands(i)
                command.Enabled = enable
                command.Checked = If(Me.workflowView.PrintPreviewMode, ((command.CommandID Is WorkflowMenuCommands.PrintPreview) OrElse (command.CommandID Is WorkflowMenuCommands.PrintPreviewPage)), (command.CommandID Is WorkflowMenuCommands.DefaultPage))
            Next i
        End Sub

        Friend Sub UpdatePanCommands(ByVal enable As Boolean)
            Dim did As CommandID = Me.ConvertMessageFilterToCommandID
            Dim navigationToolCommands As CommandSetItem() = Me.navigationToolCommands
            Dim i As Integer
            For i = 0 To navigationToolCommands.Length - 1
                Dim command As MenuCommand = navigationToolCommands(i)
                command.Enabled = enable
                command.Checked = (did Is command.CommandID)
            Next i
        End Sub

        Friend Sub UpdateZoomCommands(ByVal enable As Boolean)
            Dim num As Integer = Me.ConvertToZoomCommand(Me.workflowView.Zoom)
            Dim zoomCommands As CommandSetItem() = Me.zoomCommands
            Dim i As Integer
            For i = 0 To zoomCommands.Length - 1
                Dim command As MenuCommand = zoomCommands(i)
                command.Enabled = enable
                command.Checked = (num = command.CommandID.ID)
            Next i
        End Sub


        ' Fields
        Private activeFilter As WorkflowDesignerMessageFilter
        Private Const CF_DESIGNER As String = "CF_WINOEDESIGNERCOMPONENTS"
        Private Const CF_DESIGNERSTATE As String = "CF_WINOEDESIGNERCOMPONENTSSTATE"
        Private commandSet As List([Of] CommandSetItem)
        Private layoutCommands As CommandSetItem()
        Private menuCommandService As IMenuCommandService
        Friend Shared NavigationToolCommandIds As CommandID() = New CommandID() { WorkflowMenuCommands.ZoomIn, WorkflowMenuCommands.ZoomOut, WorkflowMenuCommands.Pan, WorkflowMenuCommands.DefaultFilter }
        Private navigationToolCommands As CommandSetItem()
        Private selectionService As ISelectionService
        Private serviceProvider As IServiceProvider
        Private workflowView As WorkflowView
        Private zoomCommands As CommandSetItem()
    End Class
End Namespace

